package com.lhy.controller;

import com.lhy.domain.DoctorAdvice;
import com.lhy.service.DoctorAdviceService;
import com.lhy.utils.Pagination;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 汪
 */
@Controller
public class DoctorAdviceController {
    @Autowired
    DoctorAdviceService doctorAdviceService;

    @RequestMapping("/showAllAdviceToDoctor")
    public String showAllAdviceToDoctor(HttpServletRequest req, HttpServletResponse resp){
        List<DoctorAdvice> advices=doctorAdviceService.showAllAdvice();
        HttpSession session = req.getSession();
        session.setAttribute("advices",advices);
        return "myyzlist";
    }
    @RequestMapping("/showAllAdviceToMember")
    public String showAllAdviceToMember(HttpServletRequest req, HttpServletResponse resp){
        List<DoctorAdvice> advices=doctorAdviceService.showAllAdvice();
        HttpSession session = req.getSession();
        session.setAttribute("advices",advices);
        return "ypfflist";
    }
    @RequestMapping("/addDoctorAdvice")
    public String addDoctorAdvice(HttpServletRequest req, HttpServletResponse resp){
        Map<String, String[]> parameterMap = req.getParameterMap();
        DoctorAdvice doctorAdvice=new DoctorAdvice();
        doctorAdvice.setDocid(1);
        doctorAdvice.setCreatedate(new Date());
        try {
            BeanUtils.populate(doctorAdvice,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        doctorAdviceService.addAdvice(doctorAdvice);
        return "main";
    }
    @RequestMapping("/deleteAdvice")
    public String deleteAdvice(HttpServletRequest req, HttpServletResponse resp){
        Integer daid = Integer.parseInt(req.getParameter("daid"));
        doctorAdviceService.deleteAdvice(daid);
        return "main";
    }
    @RequestMapping("/findSomeAdvice")
    public String findSomeAdvice(DoctorAdvice doctorAdvice, Pagination<DoctorAdvice> page, HttpServletRequest req, HttpServletResponse resp){
        page.setQueryObject(doctorAdvice);
        doctorAdviceService.findSomeAdvice(page);
        HttpSession session = req.getSession();
        session.setAttribute("advices",page);
        return "yzlist";
    }
}
